﻿@namespace ThingsGateway.Gateway.Razor
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Gateway.Application
@inherits ComponentDefault

@if (ValidateEnable)
{
    <ValidateForm Model="Model" OnValidSubmit="ValidSubmit">


        <EditorForm class="p-2" AutoGenerateAllItem="false" RowType=RowType.Inline ItemsPerRow=2 LabelWidth=250 Model="Model">
            <FieldItems>
                <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                    <EditTemplate Context="value">
                        <div class="col-12">
                            <h6>@Localizer["BasicInformation"]</h6>
                        </div>
                    </EditTemplate>
                </EditorItem>
                <EditorItem @bind-Field="@context.Name" Readonly=BatchEditEnable />
                <EditorItem @bind-Field="@context.Enable" />
                <EditorItem @bind-Field="@context.Description" />

                <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                    <EditTemplate Context="value">
                        <div class="col-12">
                            <h6>@Localizer["Connection"]</h6>
                        </div>
                    </EditTemplate>
                </EditorItem>

                <EditorItem @bind-Field="@context.ChannelId">
                    <EditTemplate Context="value">
                        <div class="col-12  col-md-6 ">
                            <BootstrapInputGroup>
                                <Select IsVirtualize @bind-Value="@value.ChannelId" Items="@Channels" ShowSearch="true" ShowLabel="true"  />
                                <Button class="text-end" Icon="fa-solid fa-plus" OnClick="AddChannel"></Button>
                            </BootstrapInputGroup>
                        </div>
                    </EditTemplate>
                </EditorItem>
                <EditorItem @bind-Field="@context.PluginName">
                    <EditTemplate Context="value">
                        <div class="col-12  col-md-6">
                            <Select @bind-Value="@value.PluginName"
                                    Items="@PluginNames" OnSelectedItemChanged=OnPluginNameChanged
                                    ShowSearch="true" >
                                <ItemTemplate Context="name">
                                    @if (PluginDcit.TryGetValue(name.Value, out var pluginOutput))
                                    {
                                        if (pluginOutput.EducationPlugin)
                                        {
                                            <div class="d-flex">
                                                <span>@name.Text</span>
                                                <div style="flex-grow: 1;"></div>
                                                <Tag Color="Color.Primary">PRO</Tag>
                                            </div>
                                        }
                                        else
                                        {
                                            <span>@name.Value</span>
                                        }
                                    }
                                    else
                                    {
                                        <span>@name.Value</span>
                                    }
                                </ItemTemplate>
                            </Select>
                        </div>
                    </EditTemplate>
                </EditorItem>
                <EditorItem @bind-Field="@context.IntervalTime" />

                <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                    <EditTemplate Context="value">
                        <div class="col-12">
                            <h6>@Localizer["Redundant"]</h6>
                        </div>
                    </EditTemplate>
                </EditorItem>
                <EditorItem @bind-Field="@context.RedundantEnable" />
                <EditorItem @bind-Field="@context.RedundantDeviceId">
                    <EditTemplate Context="value">
                        <div class="col-12  col-md-6">
                            <Select IsVirtualize @bind-Value="@value.RedundantDeviceId" style="width:100%"
                                    OnQueryAsync="OnRedundantDevicesQuery"
                                    ShowSearch="true"  IsClearable OnClearAsync=@(()=>
                                {
                                value.RedundantDeviceId = default;
                                return Task.CompletedTask;
                                })>
                                <DisplayTemplate Context="display">
                                    @{
                                        string device = "none";
                                        if (value.RedundantDeviceId != null)
                                        {
                                            if (value.RedundantDeviceId.HasValue)
                                                DeviceDict.TryGetValue(value.RedundantDeviceId.Value, out device);
                                        }
                                        @device
                                    }
                                </DisplayTemplate>
                            </Select>
                        </div>
                    </EditTemplate>

                </EditorItem>
                <div class="col-12  col-md-6" />
                <div class="col-12  col-md-6" />

                <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                    <EditTemplate Context="value">
                        <div class="col-12">
                            <h6>@Localizer["Remark"]</h6>
                        </div>
                    </EditTemplate>
                </EditorItem>
                <EditorItem @bind-Field="@context.Remark1" />
                <EditorItem @bind-Field="@context.Remark2" />
                <EditorItem @bind-Field="@context.Remark3" />
                <EditorItem @bind-Field="@context.Remark4" />
                <EditorItem @bind-Field="@context.Remark5" />
            </FieldItems>

            <Buttons>
                <Button ButtonType="ButtonType.Submit" Icon="fa-solid fa-floppy-disk" IsAsync Text=@RazorLocalizer["Save"] />
            </Buttons>

        </EditorForm>



    </ValidateForm>
}
else
{
    <Tab>
        <TabItem Text=@Localizer["DeviceInformation"]>
            <EditorForm class="p-2" AutoGenerateAllItem="false" RowType=RowType.Inline ItemsPerRow=2 LabelWidth=250 Model="Model">
                <FieldItems>
                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@Localizer["BasicInformation"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.Name" />
                    <EditorItem @bind-Field="@context.Enable" />
                    <EditorItem @bind-Field="@context.Description" />

                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@Localizer["Connection"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>

                    <EditorItem @bind-Field="@context.ChannelId">
                        <EditTemplate Context="value">
                            <div class="col-12  col-md-6 ">
                                <BootstrapInputGroup>
                                    <Select IsVirtualize @bind-Value="@value.ChannelId" Items="@Channels" ShowSearch="true" ShowLabel="true"  />
                                    <Button class="text-end" Icon="fa-solid fa-plus" OnClick="AddChannel"></Button>
                                </BootstrapInputGroup>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.PluginName">
                        <EditTemplate Context="value">
                            <div class="col-12  col-md-6">
                                <Select @bind-Value="@value.PluginName"
                                        Items="@PluginNames" OnSelectedItemChanged=OnPluginNameChanged
                                        ShowSearch="true" >
                                    <ItemTemplate Context="name">
                                        @if (PluginDcit.TryGetValue(name.Value, out var pluginOutput))
                                        {
                                            if (pluginOutput.EducationPlugin)
                                            {
                                                <div class="d-flex">
                                                    <span>@name.Text</span>
                                                    <div style="flex-grow: 1;"></div>
                                                    <Tag Color="Color.Primary">PRO</Tag>
                                                </div>
                                            }
                                            else
                                            {
                                                <span>@name.Text</span>
                                            }
                                        }
                                        else
                                        {
                                            <span>@name.Text</span>
                                        }
                                    </ItemTemplate>
                                </Select>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.IntervalTime" />

                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@Localizer["Redundant"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.RedundantEnable" />
                    <EditorItem @bind-Field="@context.RedundantDeviceId">
                        <EditTemplate Context="value">
                            <div class="col-12  col-md-6">
                                <Select IsVirtualize @bind-Value="@value.RedundantDeviceId" style="width:100%"
                                        OnQueryAsync="OnRedundantDevicesQuery"
                                        ShowSearch="true"  IsClearable OnClearAsync=@(()=>
                                    {
                                    value.RedundantDeviceId = default;
                                    return Task.CompletedTask;
                                    })>
                                    <DisplayTemplate Context="display">
                                        @{
                                            string device = "none";
                                            if (value.RedundantDeviceId != null)
                                            {
                                                DeviceDict.TryGetValue(value.RedundantDeviceId.Value, out device);
                                            }
                                            @device
                                        }
                                    </DisplayTemplate>
                                </Select>
                            </div>
                        </EditTemplate>

                    </EditorItem>
                    <div class="col-12  col-md-6" />
                    <div class="col-12  col-md-6" />

                    <EditorItem TValue="string" TModel="Device" @bind-Field="@context.Description">
                        <EditTemplate Context="value">
                            <div class="col-12">
                                <h6>@Localizer["Remark"]</h6>
                            </div>
                        </EditTemplate>
                    </EditorItem>
                    <EditorItem @bind-Field="@context.Remark1" />
                    <EditorItem @bind-Field="@context.Remark2" />
                    <EditorItem @bind-Field="@context.Remark3" />
                    <EditorItem @bind-Field="@context.Remark4" />
                    <EditorItem @bind-Field="@context.Remark5" />
                </FieldItems>
            </EditorForm>
        </TabItem>
        <TabItem Text=@Localizer["PluginInformation"]>
            @if (Model.PluginPropertyModel != null)
            {
                if (PropertyUIType == null)
                {
                    <BusinessPropertyWithCacheIntervalScriptRazor @key=Model.PluginName Model="Model" PluginPropertyEditorItems="PluginPropertyEditorItems" />
                }
                else
                {
                    @UIRenderFragment
                }
            }
        </TabItem>


    </Tab>

}


